
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <event.h>
#include <sock.h>
#include <buf.h>
#include <log.h>

#include "setting.h"
#include "client.h"

#define DEFAULT_CONF "lmail_imap_login.conf"
char g_confile[255] = {0};

struct event_t *g_event;

void usage()
{
	printf("Usage:\n");
	printf("\t-f : x config file\n");

	return;
}

void setopt(int argc, char *argv[])
{
	int opt;
	while((opt = getopt(argc, argv, "f:")) != -1) {
		switch(opt) {
			case 'f':
				strncpy(g_confile, optarg, sizeof(g_confile)-1);
				break;
			default:
				usage();
				exit(1);
				break;
		}
	}

	return;
}

int main(int argc, char *argv[])
{
	log_open("imap-login" , LOG_USER, LOG_DEBUG);
		
	setopt(argc, argv);

	if(strlen(g_confile) > 0) {
		setting_load(g_confile);
	} else {
		setting_load(DEFAULT_CONF);
	}

	client_init();

	g_event = event_create();
	if(g_event == NULL) {
		return 0;
	}

	event_call(g_event, client_timeout);
	event_add(g_event, STDIN_FILENO, EVTYPE_RD, client_input, &g_client);
	event_loop(g_event);

	LOGD("program exit");

	return 0;
}


